package com.llongwill.slg.gateway.user.appservice;

import com.llongwill.slg.gateway.user.service.ThreeHelperLoginService;
import com.llongwill.slg.gateway.user.service.UserLoginService;
import com.llongwill.slg.gateway.user.support.SjyVistorUserLoginRequest;
import com.llongwill.slg.gateway.user.support.UserLoginRequest;
import com.llongwill.slg.gateway.user.support.UserLoginResult;
import com.llongwill.slg.user.appservice.IUserQueryAppService;
import com.llongwill.slg.user.entity.UserDTO;
import com.xiangff.base.framework.resp.ResponseParam;
import com.xiangff.base.framework.util.Assert;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.llongwill.slg.gateway.common.GwReqConstants.LOGIN;


/**
 * 用户相关接口
 *
 * @author xiangff
 */
@RestController
@RequestMapping(LOGIN)
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class LoginController {

    private final UserLoginService userLoginService;

    private final ThreeHelperLoginService threeHelperLoginService;

    private final IUserQueryAppService userQueryAppService;

    /**
     * 处理用户登出请求。
     * <p>
     * 通过接收idToken和x-business-header参数，调用用户登录服务完成用户登出操作。
     * idToken用于验证用户身份，x-business-header可能包含业务相关的标识信息，用于区分不同的业务场景。
     *
     * @param idToken         用户的身份令牌，用于标识请求的用户。
     * @param xBusinessHeader 业务头部信息，用于业务区分的标识。
     * @return 返回一个表示操作成功的响应参数。
     */
    @PostMapping("/logout")
    public ResponseParam logout(@RequestParam(name = "idToken") String idToken, @RequestParam(name = "x-business-header") String xBusinessHeader) {
        // 调用用户登录服务完成用户登出操作。
        userLoginService.logout(idToken, xBusinessHeader);
        // 返回表示操作成功的响应参数。
        return ResponseParam.success();
    }

    /**
     * 三个助手token登录
     *
     * @param idToken
     * @return
     */
    @PostMapping("/threeHelper")
    public ResponseParam<UserLoginResult> threeHelperLogin(@RequestParam(name = "idToken") String idToken,
                                                           @RequestParam(name = "x-business-header") String xBusinessHeader,
                                                           @RequestParam(value = "eduStage", required = false) String eduStage,
                                                           @RequestParam(value = "eduSubject", required = false) String eduSubject) {
        UserLoginResult userLoginResult = threeHelperLoginService.loginByIdToken(idToken, xBusinessHeader,eduStage,eduSubject);
        return !userLoginResult.isLoginSuccess() ?
                ResponseParam.fail(userLoginResult.getLoginCode(), userLoginResult.getLoginMessage()) :
                ResponseParam.success(userLoginResult);
    }

    /**
     * 游客登录
     *
     * @param loginRequest
     * @return
     */
    @PostMapping("/visitor")
    public ResponseParam<UserLoginResult> visitorLogin(@RequestBody UserLoginRequest loginRequest) {
        UserLoginResult userLoginResult = userLoginService.visitorLogin(loginRequest);

        return !userLoginResult.isLoginSuccess() ?
                ResponseParam.fail(userLoginResult.getLoginCode(), userLoginResult.getLoginMessage()) :
                ResponseParam.success(userLoginResult);
    }

    /**
     * 赛教研游客登录
     *
     * @return
     */
    @PostMapping("/sjy/visitor")
    public ResponseParam<UserLoginResult> sjyVistorLogin(@RequestBody SjyVistorUserLoginRequest loginRequest) {
        UserLoginResult userLoginResult = userLoginService.sjyVisitorLogin(loginRequest);

        return !userLoginResult.isLoginSuccess() ?
                ResponseParam.fail(userLoginResult.getLoginCode(), userLoginResult.getLoginMessage()) :
                ResponseParam.success(userLoginResult);
    }

    /**
     * token登录
     *
     * @param userToken
     * @return
     */
    @PostMapping("/userToken")
    public ResponseParam<UserLoginResult> doLogin(@RequestParam(name = "userToken") String userToken, @RequestParam(name = "x-business-header") String xBusinessHeader) {
        UserLoginResult userLoginResult = userLoginService.userTokenLogin(userToken, xBusinessHeader);

        return !userLoginResult.isLoginSuccess() ?
                ResponseParam.fail(userLoginResult.getLoginCode(), userLoginResult.getLoginMessage()) :
                ResponseParam.success(userLoginResult);
    }

    /**
     * 登录
     *
     * @param loginRequest 用户
     * @return
     */
    @PostMapping(value = "/username")
    public ResponseParam<UserLoginResult> doLogin(@RequestBody @Validated UserLoginRequest loginRequest) {
        Assert.dataNExist(loginRequest.getUsername(), loginRequest.getUsername());
        Assert.dataNExist(loginRequest.getPassword(), loginRequest.getPassword());
        //执行登录
        UserLoginResult login = userLoginService.login(loginRequest.getUsername(), loginRequest.getPassword());

        return !login.isLoginSuccess() ?
                ResponseParam.fail(login.getLoginCode(), login.getLoginMessage()) :
                ResponseParam.success(login);
    }

    /**
     * 登录id登录
     *
     * @param loginId 登录id
     * @return
     */
    @PostMapping("/loginId")
    public ResponseParam<UserLoginResult> doLoginByLoginId(@RequestParam(name = "loginId") String loginId) {

        //执行登录
        UserLoginResult login = userLoginService.loginByLoginId(loginId);

        return !login.isLoginSuccess() ?
                ResponseParam.fail(login.getLoginCode(), login.getLoginMessage()) :
                ResponseParam.success(login);
    }


    /**
     * 根据id获取用户信息
     *
     * @param id 用户id
     * @return UserDTO
     */
    @GetMapping("/get")
    public ResponseParam<UserDTO> getUser(@RequestParam("id") String id) {
        return ResponseParam.success(userQueryAppService.getUser(id));

    }

    /**
     * 根据id获取用户信息
     *
     * @return UserDTO
     */
    @GetMapping("/loginUser")
    public ResponseParam<UserDTO> loginUser() {
        return ResponseParam.success(userLoginService.loginUser());

    }
}
